﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ResourceListForRole.aspx.cs"
    Inherits="Atomic.Web.Pages.Sys.ResourceListForRole" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
    <style type="text/css">
        .lblRoleFun
        {
            cursor: pointer;
            float: left;
            display: block;
            width: 120px;
        }
        .lblRoleFun:hover
        {
            color: #f00;
        }
    </style>
    <script type="text/javascript" language="javascript">
        $(function () {
            var box = $('#ResourceListForRole');
            var RoleID = '<%=Request["RoleID"] %>';
            //查询角色所有权限
            var roleFunList;
            $.post('/Handlers/Sys/RoleHandler.ashx/?method=GetResourceFunList',
            {
                RoleID: RoleID
            },
            function (data) {
                roleFunList = data; //角色所有权限
                //列出资源列表
                $('#resourceList', box).treegrid({
                    url: '/Handlers/Sys/ResourceHandler.ashx/?method=GetResourceWithFunList',
                    idField: 'ID',
                    treeField: 'Name',
                    fitColumns: true,
                    frozenColumns: [[
	                    { field: 'Name', title: '页面名称', width: 250 }
				    ]],
                    columns: [[
                        { field: 'ID', title: '基本权限', width: 120,
                            formatter: function (v) {
                                return '<label class="lblRoleFun"><input type="checkbox" value="' + v + ',0" />浏览</label>'; //value="FormID,FunID"结构，FunID为基本浏览功能时=0；
                            }
                        },
                        { field: 'Funs', title: '其他权限', width: 300,
                            formatter: function (v, r) {
                                var strHtml = '';
                                for (var i = 0; i < r.FunPoints.length; i++) {
                                    //value="FormID,FunID"结构，FunID为基本浏览功能时=0；
                                    strHtml += '<label class="lblRoleFun"><input type="checkbox" value="' + r.FunPoints[i].FormID + ',' + r.FunPoints[i].ID + '" />' + r.FunPoints[i].Name + '</label>';
                                }
                                return strHtml;
                            }
                        }
                    ]],
                    onLoadSuccess: function () {
                        //初始化复选框状态
                        $('.lblRoleFun', box).each(function () {
                            var chk = $(':checkbox', this);
                            var chkFormID = chk.attr('value').split(',')[0];    //资源ID
                            var chkFunID = chk.attr('value').split(',')[1]; //资源功能点ID
                            //选中角色已经拥有权限的资源功能点
                            for (var i = 0; i < roleFunList.length; i++) {
                                if (roleFunList[i].FormID == chkFormID && roleFunList[i].FunID == chkFunID) {
                                    chk.attr('checked', true);
                                }
                            }
                        });

                        //复选框勾选事件
                        $('.lblRoleFun', box).unbind('click');
                        $('.lblRoleFun', box).bind('click', function () {
                            var chk = $(':checkbox', this);   //点击的lbl所属的复选框
                            if (chk.attr('checked')) {
                                chk.attr('checked', false);
                                $.post('/Handlers/Sys/RoleHandler.ashx/?method=RemoveResourceFun',
                                {
                                    RoleID: RoleID,
                                    FormID: chk.attr('value').split(',')[0],
                                    FunID: chk.attr('value').split(',')[1]
                                },
                                function (data) {
                                    if (!data.Success) {
                                        $.messager.alert('提示信息', data.Message, 'warning');
                                        chk.attr('checked', true);
                                    }
                                },
                                'json');
                            } else {
                                chk.attr('checked', true);
                                $.post('/Handlers/Sys/RoleHandler.ashx/?method=AddResourceFun',
                                {
                                    RoleID: RoleID,
                                    FormID: chk.attr('value').split(',')[0],
                                    FunID: chk.attr('value').split(',')[1]
                                },
                                function (data) {
                                    if (!data.Success) {
                                        $.messager.alert('提示信息', data.Message, 'warning');
                                        chk.attr('checked', false);
                                    }
                                },
                                'json');
                            }
                        });
                    }
                });
            },
            'json');
        });
    </script>
</head>
<body>
    <div id="ResourceListForRole">
        <form id="form1" runat="server">
        <div id="resourceList">
        </div>
        </form>
    </div>
</body>
</html>
